package com.tywj.buscustomerapp.wxapi;







import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.view.activity.MyMonthTicketActivity;
import com.tywj.buscustomerapp.view.activity.MyTicketActivity;
import com.tywj.buscustomerapp.view.activity.MyTravelActivity;
import com.tywj.buscustomerapp.view.activity.PayActivity;
import com.tywj.buscustomerapp.view.activity.newBuyTicketActivity;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
    private AlertDialog.Builder builder;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_travel);
        api = WXAPIFactory.createWXAPI(this, Config.WXCODE);
        api.handleIntent(getIntent(), this);
        finish();
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d("taylor", resp.errCode+resp.errStr);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode==-1){
//				builder = new AlertDialog.Builder(this);
//				builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//				builder.show();
//				ToastUtils.show(resp.errStr+":"+resp.errCode);
				ToastJoe.getmToastJoe().ToastLongShow(this,null,"支付失败");
				finish();
			}else if (resp.errCode==-2){
				ToastJoe.getmToastJoe().ToastLongShow(WXPayEntryActivity.this,null,"支付失败");
				finish();
			}else if (resp.errCode==0){
				if (newBuyTicketActivity.activity!=null) {
					if (newBuyTicketActivity.activity.isCheck){
						startActivity(new Intent(WXPayEntryActivity.this, MyMonthTicketActivity.class));
						newBuyTicketActivity.activity.finish();
					}else {
						startActivity(new Intent(WXPayEntryActivity.this, MyTicketActivity.class));
						newBuyTicketActivity.activity.finish();
					}
				}else{
					Intent intent = new Intent(WXPayEntryActivity.this, MyTravelActivity.class);
					intent.putExtra("position",1);
					startActivity(intent);
				}
				if (PayActivity.activity!=null){
					PayActivity.activity.finish();
				}
				this.finish();
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}